package cn.zhaoyuening.hnistalumnirecord.web.controller.admin;

import cn.zhaoyuening.hnistalumnirecord.config.MyConstant;
import cn.zhaoyuening.hnistalumnirecord.dao.BaseDao;
import cn.zhaoyuening.hnistalumnirecord.domain.ConfigItem;
import cn.zhaoyuening.hnistalumnirecord.utils.ImgUploadUtils;
import cn.zhaoyuening.hnistalumnirecord.web.controller.BaseController;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by Zhao on 2017/2/22.
 * 配置信息
 * 负责后台控制首页显示内容
 */
@Controller
@RequestMapping("admin/configItem/")
public class AdminConfigItemController extends BaseController {
    @RequestMapping("configItemManagerUi.do")
    public String configItemManager(Model model) {
        ConfigItem configItem = getConfigItemService().findById(1);
        if (configItem != null) {
            model.addAttribute("configItem", configItem);
        }
        return "admin/configItemManager";
    }

    @RequestMapping("setConfigItem")
    public String setConfigItem(@ModelAttribute ConfigItem configItem, Model model) {
        configItem.setId(1);
        getConfigItemService().addOrEdit(configItem);
        model.addAttribute("result", "配置成功");
        return "admin/showResult";
    }

    /**
     * 设置首页大图 大图上传 与对应的指向地址设置
     * @param img1 大图1文件
     * @param img2
     * @param img3
     * @param address1 大图1地址
     * @param address2
     * @param address3
     * @param session
     * @param model
     * @return
     */
    @RequestMapping(value = "setIndexBigPhoto.do")
    public String setIndexBigPhoto(@RequestParam(value = "img1",required = false) MultipartFile img1,
                                   @RequestParam(value = "img2",required = false) MultipartFile img2,
                                   @RequestParam(value = "img3",required = false) MultipartFile img3,
                                   @RequestParam(value = "img1Address",required = false) String address1,
                                   @RequestParam(value = "img2Address",required = false) String address2,
                                   @RequestParam(value = "img3Address",required = false) String address3,
                                   HttpSession session,
                                   Model model
                                   ) throws IOException {
        String rootPath = session.getServletContext().getRealPath("/");
        //上传文件
        ImgUploadUtils.uploadImage(img1,rootPath, MyConstant.indexBigPhotoSavePath,"hnist1.jpg");
        ImgUploadUtils.uploadImage(img2,rootPath, MyConstant.indexBigPhotoSavePath,"hnist2.jpg");
        ImgUploadUtils.uploadImage(img3,rootPath, MyConstant.indexBigPhotoSavePath,"hnist3.jpg");

        ConfigItem configItem = getConfigItemService().findById(1);
        if (configItem==null){
            configItem = new ConfigItem();
            configItem.setId(1);
        }
        if (!StringUtils.isEmpty(address1)){
            configItem.setIndexBigPhoto1Address(address1);
        }
        if (!StringUtils.isEmpty(address2)){
            configItem.setIndexBigPhoto2Address(address2);
        }
        if (!StringUtils.isEmpty(address3)){
            configItem.setIndexBigPhoto3Address(address3);
        }
        getConfigItemService().addOrEdit(configItem);
        model.addAttribute("result", "修改大图成功！");
        return "admin/showResult";
    }
}
